package com.yj.cloudnote.mongo.dao.Impl;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;
import com.yj.cloudnote.mongo.dao.AbstractBaseMongoTemplete;
import com.yj.cloudnote.mongo.dao.DirDao;
import org.springframework.stereotype.Repository;

import javax.swing.plaf.basic.BasicBorders;
import java.util.Map;

/**
 * @Author: jy
 * @Description:
 * @Date: 10:19 2018/4/26
 */
@Repository
public class DirDaoImpl extends AbstractBaseMongoTemplete implements DirDao {

    /**
     * @Author: jy
     * @Description:
     * @Date: 10:27 2018/4/26
     */
    @Override
    public Boolean initDir(DBObject dbObject) {
        mongoTemplate.insert(dbObject,"cn_dir");
        return true;
    }

    /**
     * @Author: jy
     * @Description:
     * @Date: 10:27 2018/4/26
     */
    @Override
    public DBObject getDirOne(String username) {
        DBObject query = new BasicDBObject("username",username);
        DBObject key = new BasicDBObject("directory",1);
        return mongoTemplate.getCollection("cn_dir").findOne(query,key);
    }

    /**
     * @Author: jy
     * @Description:
     * @Date: 12:22 2018/4/26
     */
    @Override
    public DBObject getDirCount(String username) {
        DBObject query = new BasicDBObject("username",username);
        DBObject key = new BasicDBObject("dirCount",1);
        DBObject result = mongoTemplate.getCollection("cn_dir").findOne(query,key);
        return result;
    }

    /**
     * @Author: jy
     * @Description:
     * @Date: 12:37 2018/4/26
     */
    @Override
    public DBObject getFileCount(String username) {
        DBObject query = new BasicDBObject("username",username);
        DBObject key = new BasicDBObject("fileCount",1);
        DBObject result = mongoTemplate.getCollection("cn_dir").findOne(query,key);
        return result;
    }

    /**
     * @Author: jy
     * @Description:
     * @Date: 19:33 2018/4/27
     */
    @Override
    public DBObject getOne(Map<String, Object> query, Map<String, Object> key) {
        DBObject DBquery = new BasicDBObject();
        DBquery.putAll(query);
        DBObject DBkey = new BasicDBObject();
        DBkey.putAll(key);
        DBObject result = mongoTemplate.getCollection("cn_dir").findOne(DBquery,DBkey);
        return result;
    }

    /**
     * @Author: jy
     * @Description:
     * @Date: 14:15 2018/4/26
     */
    @Override
    public void updateOne(Map<String, Object> where, Map<String, Object> set) {
        DBObject DBwhere = new BasicDBObject();
        DBwhere.putAll(where);
        DBObject DBset = new BasicDBObject();
        DBObject dbObject = new BasicDBObject();
        dbObject.putAll(set);
        DBset.put("$set",dbObject);
        mongoTemplate.getCollection("cn_dir").update(DBwhere,DBset);
    }
}
